import com.jualbuy.userprofile.SecRole
import com.jualbuy.userprofile.SecUserSecRole
import com.jualbuy.userprofile.User

class BootStrap {
	
	def springSecurityService
	

    def init = { servletContext ->
		
		def userRole = SecRole.findByAuthority('ROLE_USER')?: new SecRole(authority: 'ROLE_USER').save(failOnError:true)
		def adminRole = SecRole.findByAuthority('ROLE_ADMIN')?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError:true)
		
    	def adminUser = User.findByUsername('admin')?: new User(
			username: 'admin',
			password: springSecurityService.encodePassword('admin'),
			enabled:true).save(failOnError:true)
		
		if(!adminUser.authorities.contains(adminRole)){
			SecUserSecRole.create(adminUser, adminRole)
		}
			
				
	}
    def destroy = {
    }
}
